<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{:session('cp.cpsysname')}--商户管理平台</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="{:config('webconfig.description');}">
    <meta name="keywords" content="{:config('webconfig.keywords');}">
    <meta name="author" content="runoob">
    <link rel="stylesheet" href="__STATIC_URL__/static/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="__STATIC_URL__/static/css/index.css"/>
    <link rel="stylesheet" href="__STATIC_URL__/static/css/bootstrapSwitch.css">
    <link rel="stylesheet" type="text/css" href="__STATIC_URL__/static/css/core.css?v=5"/>
    <link rel="stylesheet" type="text/css" href="__STATIC_URL__/static/css/setyxhd.css?v=5"/>
    <link rel="stylesheet" type="text/css" href="__STATIC_URL__/static/fonts/css/font-awesome.css?v=3"/>
    <link rel="stylesheet" href="__STATIC_URL__/static/css/setpaypurcards.css"/>

    <link rel="stylesheet" href="__STATIC_URL__/static/css/setcard.css"/>
    <script src="__STATIC_URL__/static/js/jquery-2.1.4.min.js"></script>
    <script src="__STATIC_URL__/static/js/setcard.js" type="text/javascript" charset="utf-8"></script>
    <script src="__STATIC_URL__/static/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="__STATIC_URL__/static/js/bootstrapSwitch.js"></script>


</head>
<body>
{include file='public/header'/}
<!------------------导航-------------------------->
<section id="content">
    {include file='public/left'/}
    <!--------------------------主要内容区域------------------------------------->
    <div class="content">
        <!--路径导航-->
        <ol class="breadcrumb">
            <li>当前位置是&nbsp;:</li>
            <li><a href="javascript:history.back();">营销工具</a></li>
            <li><a href="javascript:history.back(-2)">活动营销</a></li>
            <li>
                <a href="javascript:history.back(-1)">免费券</a>
            </li>
            <li class="active" style="color: #02b87f;">免费券修改</li>
        </ol>
        <!--创建会员卡主要内容-->
        <div class="card-content clearfix">
            <div class="pull-left card-left">
                <div class="cardL-body" style="    min-height: 625px;">
                    <div class="cardL-title" style="background: #000 url(/assets/merchant/image/APPtop.png) no-repeat center top;"></div>
                    <span class="activityTitle title">{$info['title']}</span>
                    <div class="commodityImgBox">
                        <img src="{$info['img']}" alt="商品图片" class="commodityImg">
                    </div>
                    <div class="mtop10 commodityIntroBox">
                        <span class="color1 c-inlineblock c-aligh-left"></span>
                    </div>
                    <div class="mtop5 color6 commodityUnivalenceBox">

                        <span class="commodityUnivalence goodsjianjie">{$info['goodsjianjie']}</span>
                    </div>
                    <div class="pad20">
                        <div class="c-line"></div>
                    </div>
                    <div class="commodityDescBox c-aligh-left">

                        <span class="goodsinfo">{$info['goodsinfo']}</span>

                    </div>



                </div>
            </div>



        </div>
        <div class="cardR-right">
            <div style="width: 100%;">
                <!-- TAB导航 -->

                <!-- Tab 内容 -->
                <div class="panel panel-default" style="margin-top: 15px;">
                    <form class="form-horizontal" action="" method="post" id="creatcards" onsubmit="return check()">
                        <div class="tab-content">
                            <!--基本信息-->
                            <div role="tabpanel" class="tab-pane active" id="home">

                                <div class="panel-body">
                                    <div class="form-group">
                                        <label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="red-xing-rs"></span>门店：</label>
                                        <div class="col-sm-9 col-xs-12">
                                            <select name="mid" class="form-control" id="" required>
                                                <option value="">请选择门店</option>
                                                {volist name="mach" id="mach"}
                                                <option  {if condition="$info['mid'] eq $mach['id'] "} selected {/if}" value="{$mach['id']}">{$mach['merchant']}</option>
                                                {/volist}
                                            </select>
                                            <div class="text-danger">只显示已授权微信公众号的门店</div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><span class="red-xing-rs"></span>商城标题：</label>
                                        <div class="col-sm-10">

                                            <input type="text" name="title" id="title" value="{$info['title']}" class="form-control" oninput="binding('title')" maxlength="12" required placeholder=""  />
                                        </div>
                                    </div>
                                    <div class="form-horizontal" data-label="商城生效时间"
                                         data-layout="horizontal">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label"><span class="red-xing-rs"></span>商城生效时间：</label>
                                            <div class="col-sm-10">
                                                <div class=" w60">
                                                    <input type="text" name="times" class="form-control data_ipt" value="{$info['times']}"
                                                           placeholder="请选择开始时间-结束时间" disabled id="starttime">
                                                </div>
                                                <div class="checkbox checkbox-primary permanent"
                                                     style="left: 41px;top: 4px;display: inline-block;">
                                                    <input type="hidden" name="time_type" value="" id="lasting">
                                                    <input id="permanent_ck" type="checkbox"
                                                           class="autoDelayFlag" checked>
                                                    <label for="permanent_ck">永久有效</label>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-horizontal" data-layout="horizontal">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label"><span class="red-xing-rs"></span>活动奖品：</label>
                                            <div class="col-sm-10">

                                                <div style=" margin-top:3px;">

                                                    <div class="checkbox checkbox-info checkbox-inline prize">
                                                        <input type="hidden" id="iscards" value=""
                                                               name="is_cards">

                                                        <input type="checkbox" id="cards" class="gift-input" >
                                                        <label for="cards"> 赠送优惠券 </label>
                                                        <span class="container " style="display:none;" coupontype="0">
   <a href="javascript:;" data-counts="1" id="datacount"
      style="color: #2db7f5;font-size: 13px;">添加赠送</a>
                                                            <div class="col-xs-10 col-sm-10 col-md-10 form1" >

                                        </div>
                                                            </span>
                                                    </div>


                                                </div>

                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><span class="red-xing-rs"></span>图片：</label>
                                        <div class="col-sm-10">
                                            <img src="{$info['img']}" alt="商品图片" class="img">
                                            <div class="ng-scope" style="position: relative;">
                                                <span class="btn btn-default">上传图片</span>
                                                <input type="file" id="img" value=""  onchange="upload('img','file')"/>
                                                <input type="hidden" name="img" id="logopic-upload" value="{$info['img']}" >

                                            </div>
                                            <div class="r-desc ng-binding ng-scope"> 图片建议尺寸：600像素*500像素，格式为.jpg，大小不超过2M.</div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><span class="red-xing-rs"></span>商品简介：</label>
                                        <div class="col-sm-10">
                                            <textarea name="goodsjianjie" id="goodsjianjie" class="commodityIntro p_clear" required placeholder="" oninput="binding('goodsjianjie')" >{$info['goodsjianjie']}</textarea>
                                        </div>
                                    </div>


                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><span class="red-xing-rs"></span>每人限领：
                                            </label>
                                        <div class="col-sm-10">
                                            <!--<label class="control-label ng-scope"> 1 次</label>-->
                                            <input type="number" name="homebuy" class="form-control" value="{$info['homebuy']}"/>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><span class="red-xing-rs"></span>库存数：</label>
                                        <div class="col-sm-10">
                                            <input type="number" name="num" class="form-control" id="" required placeholder="" value="{$info['num']}" />

                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><span class="red-xing-rs"></span>商品描述内容:</label>
                                        <div class="col-sm-10">
                                            <textarea name="goodsinfo" id="goodsinfo" required class="commodityIntro p_clear" oninput="binding('goodsinfo')">{$info['goodsinfo']}</textarea>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="red-xing-rs"></span>活动状态：</label>
                                        <div class="col-sm-9 col-xs-12 coupon-color">
                                            <div class="controls">
                                                <div class="switch" id="give" data-animated="true">

                                                    <input type="checkbox" name="status" id="status" value="{$info['status']}" {if condition="$info['status'] eq '1' "} checked {/if}">

                                                </div>
                                            </div>
                                            <div class="r-desc ng-binding ng-scope"> 开启后该卡券可正常使用。关注送券不受影响。</div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="red-xing-rs"></span>开启关注送券：</label>
                                        <div class="col-sm-9 col-xs-12 coupon-color">
                                            <div class="controls">
                                                <div class="switch" id="isfollow" data-animated="true">

                                                    <input type="checkbox" name="is_follow" id="is_follow" value="{$info['is_follow']}" {if condition="$info['is_follow'] eq '1' "} checked {/if}">

                                                </div>
                                            </div>
                                            <div class="r-desc ng-binding ng-scope"> 开启后可在营销工具下关注送券里选择使用。</div>
                                        </div>
                                    </div>
                                </div>
                            </div>


                        </div>
                        <div class="panel-footer">
                            <input type="hidden" name="id" id="cid" value="{$info['id']}">
                            <button type="submit"  id="creatcards-btn" class="btn btn-success">修改</button>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>

    {include file='public/footer'/}
    <!--获取门店列表-->
    <!-- Modal -->

    </div>
</section>
<script src="__STATIC_URL__/static/laydate/laydate.js"></script>
<script src="__STATIC_URL__/static/js/setyxhd.js"></script>

<script>

    laydate.render({
        elem: '#starttime',
        type: 'datetime',
        range:'-',

        change: function(value, date,endDate){ //监听日期被切换


        }
    });

</script>
<script>
    $("#isfollow").on('switch-change', function (e, data) {
        var $el = $(data.el), value = data.value;
        if(value==true){
            $("#is_follow").val(1);
        }else{
            $("#is_follow").val(0);
        }
    });

    $("#give").on('switch-change', function (e, data) {
        var $el = $(data.el), value = data.value;
        if(value==true){
            $("#status").val(1);
        }else{
            $("#status").val(0);
        }
    });
    function binding(id){
        var val = $("#"+id).val();
        if(val==''){
            val="自定义名称";
        }
        $("."+id).text(val);
    }
</script>
<script>
    function upload(file, typed) {
        var formData = new FormData();
        formData.append('file', $('#' + file)[0].files[0]);
        formData.append('other', file);
        $.ajax({
            url: "{:url('uploads/upload')}",
            type: 'POST',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
        }).done(function (res) {
            console.log(res);
            if(res.code=="0"){
                //            左侧预览图背景
                $(".commodityImg").attr("src", res.pic);
//            右侧预览图片
                $(".img").attr("src", res.pic);
//            相应隐藏input传值
                $("#logopic-upload").val(res.pic);
            }else{
                layer.alert(res.message);
            }

            // $("." + file).attr("src", res.savapath).css("display", "block");
            // $(".file-" + file).val(res.other);
            // $("." + typed + "-" + file).val(res.other);
        }).fail(function (res) {
            alert("上传失败")
        });
    };
</script>
<script>
    $(function(){
        var id=$("#cid").val();
        $.ajax({
            url: "{:url('marke/getfreecardslist')}",
            type: "post",
            data: {id:id},
            success: function (data) {
                console.log(1, data);
                var text = data;
                var html8=""
                for(var i=0; i<text.length; i++){
                    // console.log(text[i].card_id);
                    if(text[i].title==null){
                        html8=""
                    }else{
                        var html8 ='                        <div class="col-xs-12 col-sm-12 col-md-12 addcord" >\n' +
                            '                            <div class="gift-record">\n' +
                            '                                <select name="cardlist[zs_card_id][]"  class="form-control" >\n' +
                            '                                    <option value="'+text[i].card_id+'" data-title="'+text[i].title+' "data-limit="'+text[i].get_limit+'">'+text[i].title+'  ( 最大可选卡券数量为'+text[i].get_limit+')</option>\n' +
                            '                                     </select>\n' +
                            '                                <input type="number" class="form-control" min="1" placeholder="请填写" value="'+text[i].zs_no+'" name="cardlist[zs_no][]">\n' +
                            '                            </div>\n' +
                            '                             <a href="javascript:;" onclick="delsel(this)">删除</a>\n' +
                            '                    </div>';
                    }

                    $('.form1').append(html8)
                }

            }
        });
        $.ajax({
            url: "{:url('cards/getfreecard')}",
            type:"post",
            data: {
            },
            success: function (data) {
                console.log(1,data);
                var text =data.data;
                //console.log(text);
                var option2="";
                for(var i=0; i<text.length; i++){
                    // console.log(text[i].card_id);

                    option2 +='<option value="'+text[i].card_id+'" data-title="'+text[i].title+' "data-limit="'+text[i].get_limit+'">'+text[i].title+'  ( 最大可选卡券数量为'+text[i].get_limit+' )</option>\n'

                }
                $('#sel1').find("select").append(option2);
                $("#opt").change(function(){

                    limit=parseInt($("#sel1").find("option:selected").attr("data-limit"));
                    var nu=parseInt($("#num").val());

                    $("#num").attr("max",limit);
                    console.log(333333,limit);




                });

                $("#datacount").click(function(){


                    datacount=$(this).attr('data-counts');
                    get_limit=parseInt($("#sel"+datacount).find("option:selected").attr("data-limit"));
                    num=parseInt($("#sel"+datacount).find("input").val());
                    val=$("#sel"+datacount).find("option:selected").val();
                    if( $("#sel"+datacount).find("option:selected").val()==""){
                        alert("卡券选项不能为空");
                        return;
                    }
                    else if(num>get_limit){
                        console.log(num,get_limit);
                        console.log(num>get_limit);
                        alert("你选择的卡券数量超过了该卡券的领取数量!");
                        return;
                    }
                    else{

                        datacount2=parseInt(datacount)+1;
                        $(this).attr('data-counts',datacount2);
                        var html1 ='                        <div class="col-xs-12 col-sm-12 col-md-12 addcord" >\n' +
                            '                            <div class="gift-record"id="sel'+datacount2+'">\n' +
                            '                                <select name="cardlist[zs_card_id][]"  class="form-control" >\n' +
                            '                                    <option value="" selected="selected">--请选择--</option>\n' ;

                        var html3= '                                </select>\n' +
                            '                                <input type="number" class="form-control" min="1" placeholder="请填写" value="1" name="cardlist[zs_no][]">\n' +
                            '                            </div>\n' +
                            '                             <a href="javascript:;" onclick="delsel(this)">删除</a>\n' +
                            '                    </div>';

                        $('.form1').append(html1+option2+html3);

                        $("#sel"+datacount2).find("select").change(function(){

                            limit=$("#sel"+datacount2).find("option:selected").attr("data-limit");
                            $("#sel"+datacount2).find("input").attr("max",limit);



                        });

                    }




                })
            }
        })


    })
</script>
<script>
    function check(){
        var logopic=$("#logopic-upload").val();
        var opt=$("#opt").val();
        var starttime=$("#starttime").val();

        var items = document.getElementsByTagName('select'),
            len = items.length,
            i = 0,
            valMap = {},
            sel,
            val;

        // 遍历并保存各个下拉列表的值于valMap
        for(; i < len; i++){
            sel = items[i];
            val = sel.options[sel.selectedIndex].value;

            // 没有相同的取值时保存该值，否则返回false
            if(!valMap[val]){
                valMap[val] = 1;
            }else{
                alert('请删除重复的卡券！');
                return false;
            }

        }
        if(logopic==""){
            alert('请上传图片！');
            return false;
        }
        if(opt==""){
            alert('请选择卡券！');
            return false;
        }
        if(starttime==""){
            alert('请选择时间！');
            return false;
        }
        // 校验通过，全部都不相同
        return true;

    }

</script>
<script>

    function delsel(obj){
//
        $(obj).parent().remove();


    }

</script>
<script>

    //数据绑定
    function binding(id,name){
        var val = $("#"+id).val();
        if(val==""){
            val=name
        }
        $("."+id).text(val)
    }

</script>
</body>

</html>
